package org.noote.shaky_remote;

import org.noote.libs.shaky.configuration.RobotSettings;
import org.noote.libs.shaky.configuration.RobotStates;
import org.noote.libs.virtualpad.Virtual1DPad;
import org.noote.libs.virtualpad.VirtualPad_Input;
import org.noote.libs.virtualpad.VirtualTouchPad_Renderer;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

public class DisplayView extends View {
	
	Paint _paint, _paintFont, _paintFontLightBlue, _paintMessageFont, _paintLogFont, _paintCircle, _paintCircleRadar;
	
	RobotSettings _settings = null;
	RobotStates _states = null;
	
	VirtualTouchPad_Renderer _padRenderer = new VirtualTouchPad_Renderer();
	VirtualPad_Input _padInput = new VirtualPad_Input();
	
	Virtual1DPad _padLoops0 = new Virtual1DPad();
	Virtual1DPad _padLoops1 = new Virtual1DPad();
	Virtual1DPad _padLoops2 = new Virtual1DPad();
	Virtual1DPad _padLoops3 = new Virtual1DPad();
	Virtual1DPad _padLoops4 = new Virtual1DPad();
	
	Virtual1DPad _padFrame0 = new Virtual1DPad();
	Virtual1DPad _padFrame1 = new Virtual1DPad();
	Virtual1DPad _padFrame2 = new Virtual1DPad();
	Virtual1DPad _padFrame3 = new Virtual1DPad();
	Virtual1DPad _padFrame4 = new Virtual1DPad();
	Virtual1DPad _padFrame5 = new Virtual1DPad();
	Virtual1DPad _padFrame6 = new Virtual1DPad();
	Virtual1DPad _padFrame7 = new Virtual1DPad();
	Virtual1DPad _padFrame8 = new Virtual1DPad();
	Virtual1DPad _padFrame9 = new Virtual1DPad();
	Virtual1DPad _padFrame10 = new Virtual1DPad();
	
	String _sTargetMessage = "";
		
	public DisplayView(Context context) {
        super(context);
        
        _paint = new Paint();
        _paint.setColor(Color.WHITE);
        _paint.setStyle(Paint.Style.STROKE);
        
        _paintFont = new Paint();
        _paintFont.setColor(Color.WHITE);
        _paintFont.setStyle(Paint.Style.STROKE);
        _paintFont.setTextSize(20);
        _paintFont.setAntiAlias(true);
        _paintFont.setShadowLayer(2, 0, 0, Color.DKGRAY);
        
        _paintFontLightBlue = new Paint();
        _paintFontLightBlue.setColor(Color.CYAN);
        _paintFontLightBlue.setStyle(Paint.Style.STROKE);
        _paintFontLightBlue.setTextSize(20);
        _paintFontLightBlue.setAntiAlias(true);
        _paintFontLightBlue.setShadowLayer(2, 0, 0, Color.DKGRAY);

        _paintMessageFont = new Paint();
        _paintMessageFont.setColor(Color.RED);
        _paintMessageFont.setTextSize(32);
        _paintMessageFont.setAntiAlias(true);
        _paintMessageFont.setStyle(Paint.Style.STROKE);
        _paintMessageFont.setShadowLayer(3, 0, 0, Color.WHITE);
        
        _paintLogFont = new Paint();
        _paintLogFont.setColor(Color.YELLOW);
        _paintLogFont.setAntiAlias(true);
        _paintLogFont.setTextSize(12);
        _paintLogFont.setShadowLayer(1, 0, 0, Color.WHITE);
        
        _paintCircle = new Paint();
        _paintCircle.setARGB(192, 255, 255, 255);
        _paintCircle.setAntiAlias(true);
        _paintCircle.setStyle(Paint.Style.STROKE);
        
        _paintCircleRadar = new Paint();
        _paintCircleRadar.setARGB(128, 255, 255, 92);
        _paintCircleRadar.setAntiAlias(true);
        _paintCircleRadar.setStyle(Paint.Style.STROKE);
	}
	
	@Override
	protected void onSizeChanged(int width, int height, int width_old, int height_old)
	{
		int size = width>height?width:height;
        float fScale = 0.575f;
		if(size>=1000) fScale = 1.0f; // full
		else if(size>=800) fScale = 0.85f; // full
		else if(size>=500) fScale = 0.65f; // full
        else if(size<=300) fScale = 0.5f; // low
		
		_paintFont.setTextSize(20*fScale);
		_paintFontLightBlue.setTextSize(20*fScale);
		_paintMessageFont.setTextSize(32*fScale);
		_paintLogFont.setTextSize(12*fScale);
		
       	_padRenderer.loadImages(getContext(), fScale);
       	
       	_padLoops0.setWorkSpace(0, 0, _padRenderer.getWidth(_padLoops0), _padRenderer.getHeight(_padLoops0));
       	_padLoops0.setText("Stand");
       	_padInput.add(_padLoops0);

       	_padLoops1.setWorkSpace(_padRenderer.getWidth(_padLoops0), 0, _padRenderer.getWidth(_padLoops1), _padRenderer.getHeight(_padLoops1));
       	_padLoops1.setText("Hello");
       	_padInput.add(_padLoops1);

       	_padLoops2.setWorkSpace(_padRenderer.getWidth(_padLoops0)*2, 0, _padRenderer.getWidth(_padLoops2), _padRenderer.getHeight(_padLoops2));
       	_padLoops2.setText("Hulk");
       	_padInput.add(_padLoops2);

       	_padLoops3.setWorkSpace(_padRenderer.getWidth(_padLoops0)*3, 0, _padRenderer.getWidth(_padLoops3), _padRenderer.getHeight(_padLoops3));
       	_padLoops3.setText("Pump");
     	_padInput.add(_padLoops3);

       	_padLoops4.setWorkSpace(_padRenderer.getWidth(_padLoops0)*4, 0, _padRenderer.getWidth(_padLoops4), _padRenderer.getHeight(_padLoops4));
       	_padLoops4.setText("S.Pump");
       	_padInput.add(_padLoops4);

	
       	_padFrame0.setWorkSpace(0, height-_padRenderer.getHeight(_padFrame1), _padRenderer.getWidth(_padFrame0), _padRenderer.getHeight(_padFrame0));
       	_padFrame0.setText("F0");
       	_padInput.add(_padFrame0);

       	_padFrame1.setWorkSpace(_padRenderer.getWidth(_padFrame1), height-_padRenderer.getHeight(_padFrame1), _padRenderer.getWidth(_padFrame1), _padRenderer.getHeight(_padFrame1));
       	_padFrame1.setText("F1");
       	_padInput.add(_padFrame1);

       	_padFrame2.setWorkSpace(_padRenderer.getWidth(_padFrame2)*2, height-_padRenderer.getHeight(_padFrame2), _padRenderer.getWidth(_padFrame2), _padRenderer.getHeight(_padFrame2));
       	_padFrame2.setText("F2");
       	_padInput.add(_padFrame2);

       	_padFrame3.setWorkSpace(_padRenderer.getWidth(_padFrame3)*2, height-_padRenderer.getHeight(_padFrame3), _padRenderer.getWidth(_padFrame3), _padRenderer.getHeight(_padFrame3));
       	_padFrame3.setText("F3");
       	_padInput.add(_padFrame3);

       	_padFrame4.setWorkSpace(_padRenderer.getWidth(_padFrame4)*2, height-_padRenderer.getHeight(_padFrame4), _padRenderer.getWidth(_padFrame4), _padRenderer.getHeight(_padFrame4));
       	_padFrame4.setText("F4");
       	_padInput.add(_padFrame4);

       	_padFrame5.setWorkSpace(_padRenderer.getWidth(_padFrame5)*2, height-_padRenderer.getHeight(_padFrame5), _padRenderer.getWidth(_padFrame5), _padRenderer.getHeight(_padFrame5));
       	_padFrame5.setText("F5");
       	_padInput.add(_padFrame5);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
	
		{
			String sRobot;
			Rect bounds = new Rect();
			
			sRobot = String.format("%s (ver. %s)", _settings._sRobot_Name, _settings._sRobot_Version);
			_paintLogFont.getTextBounds(sRobot, 0, sRobot.length(), bounds);
			canvas.drawText(sRobot, (getWidth()-bounds.width())/2, getHeight()-(bounds.height()+5), _paintLogFont);
		}
		
		_padRenderer.draw(canvas, _padInput);
		
		if(_sTargetMessage.length()>0)
		{
			Rect bounds = new Rect();
			_paintMessageFont.getTextBounds(_sTargetMessage, 0, _sTargetMessage.length(), bounds);
			canvas.drawText(_sTargetMessage, (getWidth()-bounds.width())/2.0f, bounds.height()+5, _paintMessageFont);
		}		
	}
	
	public void setTargetMessage(String sMessage)
	{
		_sTargetMessage = sMessage;
		invalidate();
	}
}
